package mystruts;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
import java.util.ArrayList;

import org.apache.struts2.ServletActionContext;

import vo.person;

import com.opensymphony.xwork2.ActionSupport;

public class uploadAction extends ActionSupport{
	File pic1;
	String pic1ContentType;
	String pic1FileName;
	
	public File getPic1() {
		return pic1;
	}

	public void setPic1(File pic1) {
		
		this.pic1 = pic1;
	}

	public String getPic1ContentType() {
		return pic1ContentType;
	}

	public void setPic1ContentType(String pic1ContentType) {
		this.pic1ContentType = pic1ContentType;
	}

	public String getPic1FileName() {
		return pic1FileName;
	}

	public void setPic1FileName(String pic1FileName) {
		this.pic1FileName = pic1FileName;
	}

	public String execute(){
		
		return "input";
	}
	
	public String upload(){
		this.LOG.info("pic1:"+pic1);
		this.LOG.info("pic1ContentType:"+pic1ContentType);
		this.LOG.info("pic1FileName:"+pic1FileName);
		String saveDir=ServletActionContext.getServletContext().getRealPath("upload")
		+"\\"+pic1FileName;
		this.LOG.info("saveDir:"+saveDir);
		String source=pic1.getAbsolutePath();
		this.LOG.info("source:"+source);
		this.copyfile(source, saveDir);
		return "list";
	}
	
	public void copyfile(String source,String saveDir){
		try{
			FileChannel src=new FileInputStream(source).getChannel();
			FileChannel dst=new FileOutputStream(saveDir).getChannel();
			dst.transferFrom(src, 0, src.size());
			src.close();
			dst.close();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
